/**
 * Copyright (c) 2018-2022, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: Message queue
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2023-09-04     JasonHu           Init
 */

#ifndef __MSG_QUEUE__
#define __MSG_QUEUE__

#include <nxos_mini.h>
#include <base/list.h>
#include <base/semaphore.h>
#include <base/spin.h>

#define NX_MSG_QUEUE_SIZE_MAX (256U * NX_MB)
#define NX_MSG_QUEUE_COUNT_MAX (8192)

typedef struct NX_MsgBuf
{
    char *payload;
    NX_Size len;
} NX_MsgBuf;

typedef struct NX_MsgQueue
{
    NX_Size msgSizeMax;
    NX_Size msgCountMax;
    NX_List queueListHead;
    NX_Spin queueLock;
    NX_Semaphore msgSend;
    NX_Semaphore msgRecv;
} NX_MsgQueue;

NX_MsgQueue *NX_MsgQueueCreate(NX_Size msgSize, NX_Size msgCount);
NX_Error NX_MegQueueDestroy(NX_MsgQueue *msgQueue);
NX_Error NX_MsgQueueSend(NX_MsgQueue *msgQueue, NX_MsgBuf *buf);
NX_Error NX_MsgQueueTrySend(NX_MsgQueue *msgQueue, NX_MsgBuf *buf);
NX_Error NX_MsgQueueRecv(NX_MsgQueue *msgQueue, NX_MsgBuf *buf);
NX_Error NX_MsgQueueTryRecv(NX_MsgQueue *msgQueue, NX_MsgBuf *buf);
NX_Bool NX_MsgQueueFull(NX_MsgQueue *msgQueue);
NX_Bool NX_MsgQueueEmpty(NX_MsgQueue *msgQueue);
NX_Size NX_MsgQueueLenght(NX_MsgQueue *msgQueue);

#endif /* __MSG_QUEUE__ */
